package com.mlx.permission.column;


import com.mlx.permission.util.JavaBean;
import com.mlx.permission.util.MapBean;
import com.mlx.permission.entity.ColumnPermission;
import com.mlx.permission.util.PermissionUtil;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * @param <P>
 */
public abstract class AbstractColumnValidator<P extends ColumnPermission> implements IColumnValidator<P> {


    @Override
    public boolean isHavePermission(String permissionValue, String tableName) {
        // 如果是开放实体，返回true
        if (isOpenEntity()){
            return true;
        }

        // 获取权限容器
        Map<String, Set<String>> container = getContainer();

        // 判断容器中是否有指定权限
        return container.containsKey(tableName) && container.get(tableName).contains(permissionValue);
    }

    @Override
    public Map<String, Set<String>> getContainer() {
        Map<String, Set<String>> container = new HashMap<>();

        // 获取当前实体所有权限
        Collection<P> permissionCollection = getPermissionCollection();

        // 将权限放入容器
        if (PermissionUtil.isNotEmpty(permissionCollection)){
            Map<String, List<P>> permissionCollectionMap = permissionCollection.stream().collect(Collectors.groupingBy(P::getTableName));
            permissionCollectionMap.forEach((tableName, tablePermissions) ->{
                container.put(tableName, tablePermissions.stream().map(P::getPermissionValue).collect(Collectors.toSet()));
            });
        }

        return container;
    }

    @Override
    public void columnFilter(Collection<?> dataCollection, String tableName) {

        // 如果数据集合为空，结束过滤程序
        if (PermissionUtil.isEmpty(dataCollection)){
            return;
        }

        // 如果是开放实体，结束过滤程序
        if (isOpenEntity()){
            return;
        }

        // 获取权限容器
        Map<String, Set<String>> container = getContainer();

        // 获取当前实体对应数据表的列权限
        Set<String> tablePermissions = container.get(tableName);

        // 如果数据表权限为空，清空所有数据并结束过滤程序
        if (PermissionUtil.isEmpty(tablePermissions)){
            dataCollection.clear();
            return;
        }


        Object data0 = dataCollection.iterator().next();

        // 获取数据类型
        Class<?> dataClass = data0.getClass();

        // 生成数据类型JavaBean类型信息
        JavaBean javaBean = JavaBean.build(dataClass);

        // 获取javaBean所有字段
        Set<String> fieldSet = javaBean.fieldSet();

        // 遍历数据集合
        for (Object data : dataCollection){

            // 遍历数据类型字段
            for (String field : fieldSet){

                // 如果不是列权限范围内的字段，用null覆盖
                if (!tablePermissions.contains(field)){
                    javaBean.set(data, field, null);
                }
            }
        }
    }

    @Override
    public void columnFilter(Collection<?> dataCollection, String tableName, String getterName, String setterName, String fieldSetMethodName) {
        if (PermissionUtil.isEmpty(dataCollection)){
            return;
        }

        if (isOpenEntity()){
            return;
        }

        Map<String, Set<String>> container = getContainer();

        Set<String> tablePermissions = container.get(tableName);
        if (PermissionUtil.isEmpty(tablePermissions)){
            dataCollection.clear();
            return;
        }

        Object data0 = dataCollection.iterator().next();

        Class<?> dataClass = data0.getClass();

        MapBean mapBean = MapBean.build(dataClass, getterName, setterName, fieldSetMethodName);

        Collection<?> fieldSet = mapBean.fieldSet(data0);

        for (Object data : dataCollection){

            for (Object field : fieldSet){

                if (!tablePermissions.contains(field.toString())){

                    mapBean.set(data, field.toString(), null);
                }
            }
        }
    }
}
